package com.muqi.server.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.muqi.server.domain.bo.UserBO;
import com.muqi.server.domain.dto.LoginDTO;
import com.muqi.server.domain.dto.RegisterDTO;
import com.muqi.server.domain.po.UserPO;
import com.muqi.server.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Service
@RequiredArgsConstructor
public class UserService {
    private final UserMapper userMapper;
    public List<UserBO> getAllUser() {
        QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted", 0);
        return userMapper.selectList(queryWrapper).stream().map(this::convertPoTOBo).toList();
    }

    public UserBO getUserById(Integer id) {
        UserPO userPO = userMapper.selectById(id);

        return convertPoTOBo(userPO);
    }


    public Integer getUserIdByUserName(String username) {
        QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("is_deleted", 0);
        UserPO userPO = userMapper.selectOne(queryWrapper);
        if (userPO == null) {
            return -1;
        }
        return userPO.getId();
    }

    public Integer login(LoginDTO loginDTO) {
        QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", loginDTO.getUsername());
        queryWrapper.eq("password", loginDTO.getPassword());
        queryWrapper.eq("type", loginDTO.getType());
        queryWrapper.eq("is_deleted", 0);
        UserPO userPO = userMapper.selectOne(queryWrapper);
        if (userPO == null) {
            return -1;
        }
        return userPO.getId();
    }

    public Integer addUser(RegisterDTO registerDTO) {

        UserPO userPO = new UserPO();
        userPO.setNickname(registerDTO.getNickname());
        userPO.setUsername(registerDTO.getUsername());
        userPO.setPassword(registerDTO.getPassword());
        userPO.setType(registerDTO.getType());
        userPO.setIsDeleted(false);
        userPO.setCreateTime(LocalDateTime.now());
        userPO.setUpdateTime(LocalDateTime.now());
        return userMapper.insert(userPO);
    }

    public Integer deleteUser(Integer id) {
        UserPO userPO = userMapper.selectById(id);
        userPO.setIsDeleted(true);
        userPO.setUpdateTime(LocalDateTime.now());
        return userMapper.updateById(userPO);
    }



    private UserBO convertPoTOBo(UserPO userPO) {
        if (userPO == null) {
            return null;
        }
        UserBO userBO = new UserBO();
        userBO.setId(userPO.getId());
        userBO.setNickname(userPO.getNickname());

        userBO.setUsername(userPO.getUsername());
        userBO.setPassword(userPO.getPassword());
        userBO.setType(userPO.getType());
        userBO.setIsDeleted(userPO.getIsDeleted());
        userBO.setUpdateTime(userPO.getUpdateTime());
        return userBO;
    }

}
